//
//  HomeVM.swift
//  LeTu
//
//  Created by g1 on 2024/1/2.
//

import Foundation

enum ScenicClassifyType: String {
    case recommend = "推荐景点"
    case hot = "热门"
    case fiveA = "5A"
    case fourA = "4A"
//    case foreign = "国外"
}
struct ScenicClassifyItem {
    
    var classifyType: ScenicClassifyType
    
    func getRequestPatams() -> [String: Any] {
        
        var params = [String: Any]()
        
        switch classifyType {
        case .recommend:
            params["level"] = 0
        case .hot:
            params["hotspot"] = 2
        case .fiveA:
            params["level"] = 5
        case .fourA:
            params["level"] = 4
//        case .foreign:
//            params["country"] = 1
        }

        return params
    }
    
}

class HomeVM {
    
    /// 数据分页
    private var pageNum = 1
    /// 数据长度
    private(set) var pageSize = 20
    /// 关键词
    var keyword: String?
    /// 景区数据
    private(set) var sourceArr = [ScenicModel]()
    /// banner
    private(set) var banners = [ScenicModel]()
    /// 景区分类
    private(set) var classifys = [ScenicClassifyItem]()
    /// 当前选中分类
    var currentClassifysIndex = 0
    /// 城市
    var city: String = "北京".localized
    /// 国家分类 0：获取中国的所有城市，1：获取国外的所有城市
    var countryType = 0
    
    init() {
        classifys = [ScenicClassifyItem(classifyType: .recommend), ScenicClassifyItem(classifyType: .hot), ScenicClassifyItem(classifyType: .fiveA), ScenicClassifyItem(classifyType: .fourA)]//, ScenicClassifyItem(classifyType: .foreign)]
    }
    
    // MARK: - 获取banner
    func getBanner(completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .homeBanner) { [weak self] json, jsonArr, code in
            //MYLog("获取banner：", json)
                        
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]] else { return completeHandle(dialog) }
            strongSelf.banners = Mapper<ScenicModel>().mapArray(JSONArray: dataDic)
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
    // MARK: - 获取景区列表
    func getScenics(isLoad: Bool, completeHandle: @escaping NetworkRequestHandle) {
        
        var pageNum = self.pageNum
        pageNum = isLoad ? pageNum + 1 : 1
        
        var param: [String : Any] = [String : Any]()
        param["page"] = pageNum
        param["pageSize"] = pageSize
        param["city"] = city
        param["countryType"] = countryType
        
        if let keyword = keyword {
            param["keyword"] = keyword
        }
        
        if currentClassifysIndex < classifys.count {
            let classify = classifys[currentClassifysIndex]
            param = param + classify.getRequestPatams()
        }
        
        apiProvider.request(api: .getScenicList(params: param)) { [weak self] json, jsonArr, code in
            // MYLog("获取景区列表：", json)
                        
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]] else { return completeHandle(dialog) }
            
            strongSelf.pageNum = pageNum
            let tempList = Mapper<ScenicModel>().mapArray(JSONArray: dataDic)
            if isLoad {
                strongSelf.sourceArr = strongSelf.sourceArr + tempList
            } else {
                strongSelf.sourceArr = tempList
            }
            
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
}
